package com.example.thecharacterbook;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class CHECKWORD extends AppCompatActivity {
    private String Word, Mean, Example, Word_Id,Example2,Example3;
    private TextView Check_Word, Check_Mean, Check_ID, CHECK_Example,CHECK_Example2,CHECK_Example3;
    private ImageButton uk_button,en_button,EXAMPLE_BTN1,EXAMPLE_BTN2,EXAMPLE_BTN3,STOP;
    private Button DELETE;
    private Context context;
    private FloatingActionButton CheckBtn;
    private MediaPlayer mediaPlayer;
    private BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_myself);
        Intent TheCheck_intent = getIntent();
        CheckBtn=findViewById(R.id.UPDATE_FLOAT);
        Word = TheCheck_intent.getStringExtra("Word");
        Mean = TheCheck_intent.getStringExtra("Mean");
        Word_Id = TheCheck_intent.getStringExtra("Word_Id");
        Example = TheCheck_intent.getStringExtra("Example");
        Example2 = TheCheck_intent.getStringExtra("Example2");
        Example3 = TheCheck_intent.getStringExtra("Example3");
        STOP=findViewById(R.id.STOP);
        Log.v("Word", "is" + Word);
        Log.v("WordID", "is" + Word_Id);
        Log.v("Word_MEAN", "is" + Mean);
        Log.v("Word_EXAMPLE", "is" + Example);
        Log.v("Word_EXAMPLE2", "is" + Example2);
        Log.v("Word_EXAMPLE3", "is" + Example3);
        uk_button = findViewById(R.id.SoundBtn);
        en_button = findViewById(R.id.SoundBtn2);
        Check_Word = findViewById(R.id.CHECK_WORD);
        Check_Mean = findViewById(R.id.CHECK_MEAN);
        Check_ID = findViewById(R.id.CHECK_NUM);
        CHECK_Example = findViewById(R.id.CHECK_EXAMPLE1);
        CHECK_Example2 = findViewById(R.id.CHECK_EXAMPLE2);
        CHECK_Example3 = findViewById(R.id.CHECK_EXAMPLE3);
        EXAMPLE_BTN1=findViewById(R.id.SoundBtnEXAPMBLE1);
        EXAMPLE_BTN2=findViewById(R.id.SoundBtnEXAPMBLE2);
        EXAMPLE_BTN3=findViewById(R.id.SoundBtnEXAPMBLE3);
        DELETE=findViewById(R.id.CHECK_DELETE);
        Check_Word.setText(Word);
        Check_Mean.setText(Mean);
        Check_ID.setText(Word_Id);
        CHECK_Example.setText(Example);
        CHECK_Example2.setText(Example2);
        CHECK_Example3.setText(Example3);
        ActionBar ab = getSupportActionBar();
        DELETE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            REAL_DELETE();

            }
        });
        if(ab!=null){
            ab.setTitle(Word);
        }
        STOP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();
            }
        });
        CheckBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Intent intent  = new Intent(CHECKWORD.this,UpDate_PAGE.class);
                intent.putExtra("Word",Check_Word.getText().toString());
                intent.putExtra("Word_Id",Check_ID.getText().toString());
                intent.putExtra("Mean",Check_Mean.getText().toString());
                intent.putExtra("Example",CHECK_Example.getText().toString());
                intent.putExtra("Example2",CHECK_Example2.getText().toString());
                intent.putExtra("Example3",CHECK_Example3.getText().toString());
                startActivity(intent);
            }
        });
        String uk = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Word);
        String en = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "en", Word);
        String e1 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example);
        String e2 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example2);
        String e3 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example3);
       EXAMPLE_BTN1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               try {
                   mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e1));
                   mediaPlayer.start();
               } catch (Exception e) {
                   Log.e("Error,trigger!", "发音错误！");
               }
           }
       });
        EXAMPLE_BTN2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e2));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        EXAMPLE_BTN3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {

                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e3));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        en_button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               try {

                   mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(en));
                   mediaPlayer.start();
               } catch (Exception e) {
                   Log.e("Error,trigger!", "发音错误！");
               }
           }
       });
        uk_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(uk));
                    mediaPlayer.start();

                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.botton_menu, menu);
        Log.e("trueeeee!","");
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Home_menu:
                Toast.makeText(this, "May you live with ease", Toast.LENGTH_SHORT).show();
                break;
            case R.id.search:
                Toast.makeText(this, "May you live with ease", Toast.LENGTH_SHORT).show();
            case R.id.nihao:
                Toast.makeText(this, "May you live with ease", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    void REAL_DELETE(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Delete"+Word+"?");
        builder.setMessage("Are you sure you want to delete"+ Word+"?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                MyDataBase db = new MyDataBase(CHECKWORD.this);
                db.updateData(Word);
                finish();
            }
        });
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();

    }

}